import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/filter_components/widget/advanced_filter_item_container.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';
import 'package:x3_form_core/provider/base_advanced_filter_provider.dart';

/// author：william on 2021/4/15 09:55
/// email：362661044@qq.com

mixin AdvancedFilterInputMixin<T extends StatefulWidget> on State<T> {
  final InputBorder inputBorder = UnderlineInputBorder(borderSide: BorderSide(color: BaseBodyTheme.subTextColor));

  /// 全选按钮
  final BaseKeyValueModel allModel = BaseKeyValueModel(value: '全部', key: -2);

  /// 默认不全选
  bool isAll = false;

  /// 全选的 key值
  final int allKey = -2;

  /// 高度
  final double rowHeight = 32;

  /// 共享类
  BaseAdvancedFilterProvider get baseRequestFormProvider => BaseAdvancedFilterProvider.of(context, listen: false);

  @override
  void initState() {
    /// 重置监听
    baseRequestFormProvider.resetStream.listen((e) {
      resetEvent();
      if (mounted) setState(() {});
    });
    initStateEvent();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AdvancedFilterItemContainer(
      isRequired: model.required,
      title: model.name,
      child: Row(
        children: [
          BaseAdvancedSelectButton(isSelected: isAll, height: rowHeight, model: allModel, onChange: _onChange),
          SizedBox(width: 8),
          Expanded(
            child: InkWell(
              borderRadius: BorderRadius.circular(4),
              onTap: showDialog,
              child: buildContent(),
            ),
          ),
        ],
      ),
    );
  }

  /// 文本框
  Widget buildTextContainer(String value) {
    TextStyle style = value.isNotEmpty == true ? BaseStyles.subtitle2(context) : Theme.of(context).inputDecorationTheme.hintStyle;
    String _text = value.isNotEmpty ? value : '请输入';
    return Container(
      height: 28,
      padding: EdgeInsets.symmetric(horizontal: 8),
      decoration: BoxDecoration(border: Border.all(width: 1, color: BaseBodyTheme.greyBackgroundColor), borderRadius: BorderRadius.circular(4)),
      child: Align(alignment: Alignment.centerLeft, child: Text(_text, style: style)),
    );
  }

  /// 弹窗事件
  void showDialogEvent({@required String title, @required Widget child, @required VoidCallback onOKPress}) {
    BaseComponentUtils.showCommonDialog(
      context,
      titleWidget: Container(padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8), child: Text(title)),
      contentWidget: child,
      onOKPress: onOKPress,
    );
  }

  /// 全选按钮事件
  void _onChange(bool v) {
    if (v == true) {
      resetEvent();
      baseRequestFormProvider.setDataSource(model, allModel.toJson());
    }
    isAll = v;
    setState(() {});
  }

  /// **** 需要调用时实现

  /// 模型
  BaseAdvancedFilterModel get model;

  /// 初始化事件
  void initStateEvent();

  /// 内容
  Widget buildContent();

  /// 弹窗
  void showDialog();

  /// 重置事件
  void resetEvent();
}
